package org.pure.ioc.scanner;

import org.pure.util.PropertiesUtil;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by will.lin on 2015/8/10.
 */
public class Scanner {

    private static final String SCANNER_PACKAGE= PropertiesUtil.getProperty("scanner_package");  //需要被扫描的包，将这个包中的带有bean的类


    public static List<Class<?>> classList;

    static{
        try {
            classList=getClassList();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static   List<Class<?>> getClassList() throws IOException {
        List<Class<?>> classList=new ArrayList<Class<?>>();
        Enumeration<URL> urlEnumeration= Scanner.class.getClassLoader().getResources(SCANNER_PACKAGE.replace(".","/"));


        while(urlEnumeration.hasMoreElements()){
              URL url=  urlEnumeration.nextElement();
              if(url.getProtocol().equals("file")){
                  // 若在 class 目录中，则执行添加类操作
                  String packagePath = url.getPath().replaceAll("%20", " ");   //处理空格
//                  System.out.println(packagePath);
                  addClass(classList, packagePath, SCANNER_PACKAGE);
              }
        }
        return classList;
    }

    private static void addClass(List<Class<?>> classList, String packagePath, String packageName) {
        try {
            // 获取包名路径下的 class 文件或目录
            File[] files = new File(packagePath).listFiles(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory();
                }
            });
            // 遍历文件或目录
            for (File file : files) {
                String fileName = file.getName();
                // 判断是否为文件或目录
                if (file.isFile()) {
                    // 获取类名
                    String className = fileName.substring(0, fileName.lastIndexOf("."));
                    if (packageName!=null||packageName!="") {
                        className = packageName + "." + className;
                    }
                    // 执行添加类操作
                    doAddClass(classList, className);
                } else {
                    // 获取子包
                    String subPackagePath = fileName;
                    if (packagePath!=null||packagePath!="") {
                        subPackagePath = packagePath + "/" + subPackagePath;
                    }
                    // 子包名
                    String subPackageName = fileName;
                    if (packageName!=null||packageName!="") {
                        subPackageName = packageName + "." + subPackageName;
                    }
                    // 递归调用
                    addClass(classList, subPackagePath, subPackageName);
                }
            }
        } catch (Exception e) {

        }
    }

    private static void doAddClass(List<Class<?>> classList, String className) throws ClassNotFoundException {
        // 加载类

        Class<?> cls=  Class.forName(className,false,Scanner.class.getClassLoader());
        // 判断是否可以添加类
        classList.add(cls);

    }
}
